﻿using GalaSoft.MvvmLight.Messaging;
using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using TransControlSystem.ViewModel.Common;

namespace TransControlSystem.View.Common
{
    /// <summary>
    /// LogWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LogWindow : Window
    {
        private LogViewModel logViewModel = null;

        [Obsolete]
        public LogWindow()
        {
            InitializeComponent();
            Messenger.Default.Register<NotificationMessage>(this, (nm) =>
            {
                if (nm.Notification == "HideLogWindow")
                {
                    if (nm.Sender == DataContext)
                        Hide();
                }
            });
            txt_StartTime.DateTime = Convert.ToDateTime("0001-01-01 00:00:00");
            txt_EndTime.DateTime = Convert.ToDateTime("0001-01-01 00:00:00");
            logViewModel = (LogViewModel)DataContext;
            Thread SynchronizePropertyValuesThread = new Thread(new ThreadStart(SynchronizePropertyValues))
            {
                IsBackground = true,
                Priority = ThreadPriority.AboveNormal,
                ApartmentState = ApartmentState.STA
            };
            SynchronizePropertyValuesThread.Start();
        }

        private void SynchronizePropertyValues()
        {
            while (true)
            {
                if (txt_StartTime.DateTime != Convert.ToDateTime("0001-01-01 00:00:00"))
                {
                    logViewModel.StartTime = txt_StartTime.DateTime.ToString();
                }

                if (txt_EndTime.DateTime != Convert.ToDateTime("0001-01-01 00:00:00"))
                {
                    logViewModel.EndTime = txt_EndTime.DateTime.ToString();
                }
                Thread.Sleep(10);
            }
        }
        private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Column.Header.ToString() == "Id" ||
                e.Column.Header.ToString() == "UserName" ||
                e.Column.Header.ToString() == "Account" ||
                e.Column.Header.ToString() == "OrderNo" ||
                e.Column.Header.ToString() == "OperName" ||
                e.Column.Header.ToString() == "OperContent" ||
                e.Column.Header.ToString() == "OperTime" ||
                e.Column.Header.ToString() == "OperTimeText" ||
                e.Column.Header.ToString() == "DataOrderNo" ||
                e.Column.Header.ToString() == "OperIp"
              )
            {
                e.Cancel = true;
            }
        }

        private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }
    }
}
